//MAJOR TODO s:
/*
	-Make it smart enough to figure out how long the pattern should be (may
	or may not be feasible...)

	-Maybe some higher level functionality would be good. I.E. line
	drawing, etc...
*/


#include <stdio.h>
#include <inttypes.h>

#define T_GS 		1 //period of grayscale cycle in ms
#define TIME		32  //length of pattern in grayscale cycles
#define NUM_TLC		2  //number of TLCs chained together

//Function prototypes
void led_write(uint8_t led, int brightness, int time);
int delay_cycles(int t);
void export_results(void);

//Function globals
int time_ref = 0; 

//Array for the pattern
uint8_t pattern[TIME][24*NUM_TLC];

//File-related
FILE *ofp;
char filename[32];

int main(void)
{
	int choice;

	printf("Welcome to LED Cube pattern generator.\n");
	
	//Menu
	while(1) {
		printf("Main menu. Please make a selection:\n");
		printf("\t1. Create a new pattern.\n");
		printf("\t2. Edit an existing pattern.\n");
		printf("\t3. Flash pattern.\n");
		printf("\t4. Exit.\n");

		scanf("%d", &choice);
		
		if(choice == 1) {
			//Choose a filename
			printf("Filename: ");
			scanf("%s", &filename);

			//Present blank pattern

		}

		if(choice == 2) {
			//Prompt for and open file
			printf("Filename: ");
			scanf("%s", &filename);
			fopen = (filename, "r+");

			//Editing menu
		}

		if(choice == 3) {
			//Flashing code...
		}

		if(choice == 4) {
			printf("Goodbye.\n");
			return 0;
		}

		else {
			printf("Please enter a valid choice.\n");
		}
	}
}

//Most fundamental tool for building patterns is ability to write individual
//LED brightness for variable length of time.
void led_write(uint8_t led, int brightness, int time)
{
	//Saves the time for use as reference
	int current_time = time_ref;

	//n is the contiguous byte belonging to the LED - see notebook
	uint8_t n = (2*led) - (led/2);

	for (; time_ref < current_time + delay_cycles(time); ++time_ref) {
		
		//If led is 0 or even
		if ((led & 1) == 0) {
			pattern[time_ref][n] = brightness >> 4; //Full byte
			pattern[time_ref][n+1] |= ((brightness & 0x00F) << 4); //Nibble
		}

		else {
			pattern[time_ref][n-1] |= ((brightness & 0xF00) >> 8); //Nibble
			pattern[time_ref][n] = brightness & 0xFF; //Full byte
		}
	}
	//Restores the time, so it can be manually changed as needed.
	//Otherwise, it will be impossible to turn on multiple lights simultaneously.
	time_ref = current_time;
}

//Takes t in ms
int delay_cycles(int t)
{
	return(t/T_GS);
}

//I am dangerously close to scrapping this, it's a monstrosity, and so wrong
void export_results(void)
{
	//Export the results
	ofp = fopen(filename, "w");
	fprintf(ofp, "uint8_t pattern[32][48] = {\n\t\t\t  ");

	for (int t = 1; t < TIME; ++t) {



		fprintf(ofp, "{");
		for(int i = 0; i < 24; ++i) {
			fprintf(ofp, "%3d, ", pattern[t][i]);
		}

		fprintf(ofp, "\n\t\t\t   ");

		if (t == (TIME - 1)) {

			for(int i = 24; i < 48-1; ++i) {
				fprintf(ofp, "%3d, ", pattern[t][i]);
			}

			fprintf(ofp, "%3d}}\n\t\t\t  ", pattern[t][47]);
		}

		else {

			for(int i = 24; i < 48-1; ++i) {
				fprintf(ofp, "%3d, ", pattern[t][i]);
			}

			fprintf(ofp, "%3d},\n\t\t\t  ", pattern[t][47]);
		}
	}
	

	fclose(ofp);
}
